Added PackedIntArray, PackedIntSlice to std #2422
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PackedIntArray(comptime Int: type, comptime int_count: usize) type
: Creates a bit-packed array ofint_count
integers of typeInt
. Bits are packed using native endianess and without storing any meta data. PackedIntArray(i3, 8) will occupy exactly 3 bytes of memory.PackedIntSlice(comptime Int: type) type
: Uses a slice as a bit-packed block of integers of type Int. Bits are packed using native endianess and without storing any meta data..len()
gets the number of items the array/slice can hold..get(index: usize)
and.set(index: usize, int: Int)
are used for accessing the individual items..slice(start: usize, end: usize)
creates a new PackedIntSlice(Int) instance covering the specified range of items (exclusive)..sliceCast(NewInt: type)
returns a new PackedIntSlice(NewInt) covering the same bytes but using a different bit-width integer (which must fit evenly within the old range of bits).PackedIntArrays can be initialized from unpacked arrays or literals. PackedIntSlices initialize with a []u8 and a count of items.
.bytesRequired(int_count: usize)
can be used to calculate the number of bytes required in the slice for the given count.Update: there are now also
PackedIntArrayEndian
,PackedIntSliceEndian
, and.sliceCastEndian
which allow specifying the endianess of the bit packing instead of using native.